import 'package:flutter/material.dart';
import 'package:vega/page/main_page.dart';
import 'package:vega/page/search_page.dart';

class StartPage extends StatefulWidget {
  const StartPage({super.key});

  @override
  State<StatefulWidget> createState() => _StartPageState();
}

class _StartPageState extends State<StartPage> {
  bool _isSplash = true;

  @override
  Widget build(BuildContext context) {
    return _isSplash
        ? SplashPage(() => setState(() => _isSplash = false))
        : const MainPage(title: "vega");
  }
}

class SplashPage extends StatefulWidget {
  final VoidCallback cb;

  const SplashPage(this.cb, {super.key});

  @override
  State<StatefulWidget> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  int _countdown = 1;

  void _countdownFunc() {
    setState(() {
      _countdown--;
    });
    if (_countdown > 0) {
      Future.delayed(const Duration(seconds: 1), _countdownFunc);
    } else {
      widget.cb();
    }
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(seconds: 1), _countdownFunc);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(color: Colors.white),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Text(
            "Vega $_countdown sec",
            style: TextStyle(color: Colors.black),
          )
        ],
      ),
    );
  }
}
